#include <windows.h>

#include <iostream>
#include <fstream>

#include "afxsock.h"

using namespace std;

void ErrorInfo();

int main()
{	
    ofstream ofs;
    ofs.open("log.txt");

    WSADATA wsaData;

    if ( !AfxSocketInit(&wsaData) )
    {
        cerr << "winsock not be initialized !" << endl;
        return -1;
    }

    CSocket *sa;
    UINT uPort;
    UINT rc;

    for (uPort=100; uPort < 120; uPort++)
    {
        cout << "Connect to " << "pop.yandex.ru:" << uPort << "...";

        sa = new CSocket();
        sa->Create();

        rc = sa->Connect("pop.yandex.ru", uPort);
        if ( rc ) {
           cout << "Open." << endl;
           ofs << "www.yandex.ru:" << uPort << endl;
        } else {
           ErrorInfo();
        }

        sa->Close();
    }

    ofs.close(); 
}

void ErrorInfo()
{
	int i=WSAGetLastError();
	int g;
	switch (i)
	{
	case WSANOTINITIALISED:
		cout << "Not start WinSock" << endl;
		break;
	   	case WSAENETDOWN:
		cout << "NetWork failed" << endl;
		break;
	case WSAEADDRINUSE:
		cout << "Socket in use " << endl;
		break;
    	case WSAEINTR:
		cout << "call  WSACancelBlockingCall" << endl;
		break;
    	case WSAEADDRNOTAVAIL:
		cout << "address not valid " << endl;
		break;
	case WSAETIMEDOUT:
		cout << "connection Time Out " << endl;
		break;
    	case WSAENOTSOCK:
		cout << "Not Socket " << endl;
		break;
    	case WSAECONNREFUSED:
		cout << "reject connection" << endl;
		break;
	default:
		cout << " ????? " << g << endl;
		cin >> g;
		break;
	}
}